/**
 * 
 */
package sa.edu.kaust.msc.cloud.mgmt.sim.mgmt;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;

import sa.edu.kaust.msc.cloud.mgmt.sim.domain.Server;

/**
 * @author suzan
 * 
 * {@code} Datacenter 
 *
 */
public class DataCenter {
	
	private List<Host> hostList;
	private String name;
	private Datacenter datacenter = null;
	
	public DataCenter(String dataCenterName){
		hostList = new ArrayList<Host>();
		this.name = dataCenterName;
	}
	
	/**
	 * 
	 * @param servers
	 */
	public void processServers(final List<Server> servers){
		
		for(Server server: servers){
			addServer(server);
		}
		
		createDatacenterCharacteristics();
	}
	
	/**
	 * 
	 * @param server
	 */
	private void addServer(final Server server){
		int hostId = server.getHostId();
		int ram = server.getRam();
		long storage = server.getStorage();
		int bw = server.getBw();
		
		List<Pe> coreList = server.getCoreList();
		
		Host host = new Host(hostId,
				new RamProvisionerSimple(ram),
				new BwProvisionerSimple(bw),
				storage,
				coreList,
				new VmSchedulerTimeShared(coreList));
		
		hostList.add(host);
	}
	
	/**
	 * 
	 */
	private void createDatacenterCharacteristics(){
		String arch = "x86";      // system architecture
		String os = "Linux";          // operating system
		String vmm = "Xen";
		double time_zone = 10.0;         // time zone this resource located
		double cost = 3.0;              // the cost of using processing in this resource
		double costPerMem = 0.05;		// the cost of using memory in this resource
		double costPerStorage = 0.1;	// the cost of using storage in this resource
		double costPerBw = 0.1;			// the cost of using bw in this resource
		LinkedList<Storage> storageList = new LinkedList<Storage>();	//we are not adding SAN devices by now

		DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
                arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);

		// 6. Finally, we need to create a PowerDatacenter object.
		try {
			datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @return
	 */
	public List<Host> getHostList() {
		return hostList;
	}

	/**
	 * 
	 * @return
	 */
	public Datacenter getDatacenter() {
		return datacenter;
	}
	
	
}
